local lor = require("lor.index")
local topoRouter = lor:Router() -- 生成一个group router对象
local mysql = require("app.util.mysql_pool")

-----------------------------------------------------------------------------
-----------------------------------页面跳转-----------------------------------
-----------------------------------------------------------------------------

-- 查询所有产品
topoRouter:get("/query", function(req, res, next)
    local sql = "SELECT `name`,`cpu`,`memory`,`concurrent` FROM base_info WHERE del=1"
    local sql_ref = "SELECT ref.p_id,bp.NAME AS pname,ref.c_id,bc.NAME AS cname FROM ref_base ref, base_info bp, base_info bc WHERE ref.p_id = bp.id AND ref.c_id = bc.id"
    local flag,result,sqlstate, err, errno = mysql:query(sql,true)
    local flag_ref,result_ref,sqlstate_ref, err_ref, errno_ref = mysql:query(sql_ref,true)
    -- 渲染页面
    res:render("topo/info", {
        success = flag,
        message = "",
        data = result,
        links = result_ref
    })
end)

-----------------------------------------------------------------------------
-----------------------------------业务处理-----------------------------------
-----------------------------------------------------------------------------

-- 获取类别信息
topoRouter:get("/tags", function(req, res, next)
    local sql = "SELECT `name` FROM dict_tags"
    local flag,result,sqlstate, err, errno = mysql:query(sql,true)
    res:json({
        success = flag,
        message = "点多",
        data = result
    })
end)

-- 获取节点信息
topoRouter:get("/items", function(req, res, next)
    local sql = "SELECT a.*,b.name as dict_name FROM base_info a,dict_tags b WHERE a.del=1 AND a.tags=b.id"
    local flag,result,sqlstate, err, errno = mysql:query(sql,true)
    res:json({
        success = flag,
        message = "",
        data = result
    })
end)

-- 获取基础节点之间的关系
topoRouter:get("/links", function(req, res, next)
    local sql = "SELECT dic.name as dict_name,ref.p_id,bp.NAME AS pname,ref.c_id,bc.NAME AS cname FROM ref_base ref, base_info bp, base_info bc,dict_tags dic WHERE ref.p_id = bp.id AND ref.c_id = bc.id AND dic.id=bp.tags"
    local flag,result,sqlstate, err, errno = mysql:query(sql,true)
    res:json({
        success = flag,
        message = "",
        data = result
    })
end)

return topoRouter